package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.vo.PromotionItemWithMaterial;
import com.jsh.erp.service.WorkflowConfig.WorkflowConfigService;
import com.jsh.erp.service.rebate.RebateService;
import com.jsh.erp.service.rebateMaterial.RebateMaterialService;
import com.jsh.erp.service.rebateRule.RebateRuleService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;

@RestController
@RequestMapping(value = "/rebate", produces = "application/json;charset=UTF-8")
@Api(tags = {"返利管理"})
public class RebateController {


    @Resource
    private RebateService rebateService;

    @Resource
    private RebateMaterialService rebateMaterialService;

    @Resource
    private RebateRuleService rebateRuleService;

    @Resource
    private SupplierService supplierService;
    @Resource
    private RedisService redisService;

    @GetMapping(value = "/rebateDetails")
    @ResponseBody
    @ApiOperation(value = "返利明细", notes = "返利明细")
    public Object rebateDetails(@RequestParam(value = "supplierId",required = false)@ApiParam("供应商id") String supplierId,
                                @RequestParam(defaultValue = "1",required = false)@ApiParam("页码") Integer pageNum,
                                @RequestParam(defaultValue = "10",required = false)@ApiParam("显示条数") Integer pageSize,
                                HttpServletRequest request) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        List<RebateVo> vo = rebateService.rebateDetails(supplierId,(pageNum-1)*pageSize,pageSize);
        Long total = rebateService.rebateDetailsCount(supplierId);
        map.put("rows", vo);
        map.put("total", total);
        return vo;
    }

    @GetMapping(value = "/getDetailList")
    @ApiOperation(value = "明细列表")
    public BaseResponseInfo getDetailList(@RequestParam("headerId") String headerId, HttpServletRequest request) throws  Exception{
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            Rebate rebate = rebateService.getRebate(headerId);
            if (rebate!=null){
                Supplier supplier = supplierService.getSupplier(rebate.getSupplierId());
                if (supplier!=null){
                    rebate.setSupplierName(supplier.getSupplier());
                }
            }
            List<RebateItem> itemList = rebateService.getRebateItemByRebateId(headerId);
//            List<RebateRule> ruleList = rebateService.getRebateRuleByRebateId(headerId);
//            List<RebateMaterial> materialList = rebateService.getRebateMaterialByRebateId(headerId);
            JSONObject outer = new JSONObject();
            outer.put("rows", rebate);
            outer.put("meList",itemList);
//            outer.put("ruleList",ruleList);
//            outer.put("materialList",materialList);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    @PostMapping(value = "/batchSetRebateTenant")
    @ApiOperation(value = "批量设置商品下发的租户")
    public String batchSetRebateTenant(@RequestBody JSONObject jsonObject,
                                          HttpServletRequest request)throws Exception {
        String ids = jsonObject.getString("ids");
        String syncTenantId = jsonObject.getString("syncTenantId");
        Map<String, Object> objectMap = new HashMap<>();
        String lanCode = redisService.getLanCodeFromSession(request);
        int res = rebateService.batchSetRebateTenant(ids,syncTenantId);
        if(res > 0) {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.OK.name : ErpInfo.OK.lanCodeName, ErpInfo.OK.code);
        } else {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.ERROR.name : ErpInfo.ERROR.lanCodeName, ErpInfo.ERROR.code);
        }
    }
}
